local hx__hunfu = fk.CreateSkill {
  name = "hx__hunfu",
  tags = { Skill.Compulsory },
}


hx__hunfu:addEffect(fk.FinishJudge, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(hx__hunfu.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = target.room
    room:addPlayerMark(target, "@hx__hunfu")
  end,
})

hx__hunfu:addEffect(fk.Death, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self, false, true) then
      local availableTargets = {}
      local n = 0
      for _, p in ipairs(player.room.alive_players) do
        if p:getMark("@hx__hunfu") > n then
          availableTargets = {}
          table.insert(availableTargets, p.id)
          n = p:getMark("@hx__hunfu")
        elseif p:getMark("@hx__hunfu") == n then
          table.insert(availableTargets, p.id)
        end
      end
      if #availableTargets > 0 then
        event:setCostData(self, availableTargets)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "@hx__hunfu", 0)
    end
    room:doIndicate(player, event:getCostData(self))
    for _, pid in ipairs(event:getCostData(self)) do
      local p = room:getPlayerById(pid)
      room:addPlayerMark(p, "hx__hunfucontrolled")
      player:control(p)
      p:gainAnExtraTurn()
    end
  end,
})

hx__hunfu:addEffect(fk.RoundEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self, false, true) then
      local availableTargets = {}
      local n = 0
      for _, p in ipairs(player.room.alive_players) do
        if p:getMark("@hx__hunfu") > n then
          availableTargets = {}
          table.insert(availableTargets, p.id)
          n = p:getMark("@hx__hunfu")
        elseif p:getMark("@hx__hunfu") == n then
          table.insert(availableTargets, p.id)
        end
      end
      if #availableTargets > 0 then
        event:setCostData(self, availableTargets)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, event:getCostData(self))
    for _, pid in ipairs(event:getCostData(self)) do
      local p = room:getPlayerById(pid)
      room:addPlayerMark(p, "hx__hunfucontrolled")
      player:control(p)
      p:gainAnExtraTurn()
    end
  end,
})

hx__hunfu:addEffect(fk.TurnEnd, {
  can_refresh = function(self, event, target, player, data)
    return target == player and target:getMark("hx__hunfucontrolled") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = target.room
    room:setPlayerMark(target, "hx__hunfucontrolled", 0)
    target:control(target)
  end,
})

hx__hunfu:addEffect(fk.GameOverJudge, {
  can_refresh = function (self, event, target, player, data)
    return target == player and Fk.game_modes[player.room.settings.gameMode]:getWinner(target) ~= ""
  end,
  on_refresh = function(self, event, target, player, data)
    local room = target.room
    for _, p in ipairs(room.players) do
      p:control(p)
    end
  end,
})

return hx__hunfu
